// Zepto
var $ = require('./components/zepto/zepto');
require('./components/zepto/event');
require('./components/zepto/ajax');
require('./components/zepto/form');
require('./components/zepto/ie');
require('./components/zepto/touch');

module.exports = $;

var Swiper = require('./components/swiper/swiper.min');
var swiperAnimate = require('./components/swiper/swiper.animate1.0.2.min');
var IScroll = require('./components/iscroll/iscroll');
var wx = require('./components/weixin/jweixin');
//var demoUtils = require('./components/iscroll/demoUtils');

function isPassive() {
    var supportsPassiveOption = false;
    try {
        addEventListener("test", null, Object.defineProperty({}, 'passive', {
            get: function() {
                supportsPassiveOption = true;
            }
        }));
    } catch (e) {}
    return supportsPassiveOption;
}



var myScroll;

function loaded() {
    myScroll = new IScroll('#wrapper', { useTransition: false });
}
loaded();

document.addEventListener('touchmove', function(e) { e.preventDefault(); }, isPassive() ? {
    capture: false,
    passive: false
} : false);



$('.mainIscroll').hide();
$('#enterBtn').on('tap', function() {
    $('.swiper-container').hide();
    $('.mainIscroll').show();
    myScroll.refresh();
    $.ajax({
        url: 'http://www.bosslu.online/php/getsign.php',
        type: 'POST',
        data: {
            url: window.location.href
        },
        dataType: 'json',
        success: function(res) {
            wx.config({
                debug: true,
                appId: res.appId,
                timestamp: res.timestamp,
                nonceStr: res.nonceStr,
                signature: res.signature,
                jsApiList: [
                    // 所有要调用的 API 都要加到这个列表中
                    'scanQRCode',
                    'getLocation'
                ]
            });
        }

    })
    $('#scan').on('tap', function() {
        wx.scanQRCode({
            needResult: 0, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
            scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
            success: function(res) {
                var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
            }
        });
    })
    $('#location').on('tap', function() {
        wx.getLocation({
            type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
            success: function(res) {
                var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
                var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
                var speed = res.speed; // 速度，以米/每秒
                计
                var accuracy = res.accuracy; // 位置精度
            }
        });
    })

    $.ajax({
        type: "get",
        url: "http://www.bosslu.online/api/skill.php",
        dataType: 'json',
        async: true,
        success: function(data) {
            var html = '';
            for (var i = 0; i < data.length; i++) {
                html += '<li class="animated">';
                html += '<div class="img animated fadeInLeftBig">';
                html += '<img src="./images/' + data[i].image + '.png" alt="">';
                html += '</div>';
                html += '<div class="ppt animated fadeInRightBig">';
                html += '<h3>' + data[i].category + '</h3>';
                html += '<div class="sk-text">' + data[i].name + '</div>';
                html += '<div class="sk-level">' + data[i].level + '</div>';
                html += '</div>';
                html += '</li>';
            }
            $("#scroller ul").html(html);
            myScroll.refresh();
        }
    });

})


var interval = setInterval(function() {
    if (document.readyState === 'complete') {
        clearInterval(interval);
        $("#loading").hide();
        $(".swiper-container").show();
        swiper.updateContainerSize(); // 万分重要
        swiper.updateSlidesSize(); // 万分重要
    } else {
        $("#loading").show();
    }
})







$('#footer li').on('tap', function() {
    var apiTarget = $(this).attr('id');
    var apiUrl = "http://www.bosslu.online/api/" + apiTarget + ".php";
    $(this).find('i,span').addClass('active');
    $(this).siblings().find('i,span').removeClass('active');
    $('#hd').html(apiTarget);
    if (apiTarget == 'me') {
        $.ajax({
            type: "get",
            url: apiUrl,
            dataType: 'json',
            async: true,
            success: function(data) {
                //console.log(data.length)
                var html = '';

                html += '<li class="me-wrap">';
                html += '<div class="me-img animated bounceInUp">';
                html += '<img src="./images/' + data[0].image + '.png" alt="">';
                html += '</div>';
                html += '<div class="me-ppt animated bounce">';
                html += '<h3>' + data[0].name + '</h3>';
                html += '<div class="sk-add">籍贯：江西萍乡</div>';
                html += '<div class="me-bth">出生年月：1991年9月</div>';
                html += '<div class="me-like animated bounceInUp">爱好：运动、王者荣耀</div>';
                html += '<div class="me-sexs">性格：开朗、轻微强迫症</div>';
                html += '<div class="me-pro">专业：管理学/web前端</div>';
                html += '<div class="me-school">毕业院校：江西农业大学</div>';
                html += '<div class="me-tel">微信/Tel：17681825068</div>';
                html += '</div>';
                html += '</li>';

                $("#scroller ul").html(html);
                myScroll.refresh();
            }
        });
    } else if (apiTarget == 'project') {
        $.ajax({
            type: "get",
            url: apiUrl,
            dataType: 'json',
            async: true,
            success: function(data) {
                //console.log(data.length)
                var html = '';
                for (var i = 0; i < data.length; i++) {
                    html += '<li class="pjt animated zoomInDown">';
                    html += '<div class="pjt-img">';
                    html += '<img src="./images/' + data[i].image + '.png" alt="">';
                    html += '</div>';
                    html += '<div class="pjt-ppt">';
                    html += '<h3>' + data[i].category + '</h3>';
                    html += '<div class="pjt-text">' + data[i].name + '</div>';
                    html += '<div class="pjt-level">' + data[i].level + '</div>';
                    html += '</div>';
                    html += '</li>';
                }
                $("#scroller ul").html(html);
                myScroll.refresh();
            }
        });
    } else if (apiTarget == 'experience') {
        $.ajax({
            type: "get",
            url: apiUrl,
            dataType: 'json',
            async: true,
            success: function(data) {
                //console.log(data.length)
                var html = '';
                for (var i = 0; i < data.length; i++) {
                    html += '<li class="exp animated bounceInUp">';
                    html += '<div class="exp-img">';
                    html += '<img src="./images/' + data[i].image + '.png" alt="">';
                    html += '</div>';
                    html += '<div class="exp-ppt">';
                    html += '<h3>' + data[i].category + '</h3>';
                    html += '<div class="exp-text">' + data[i].time + '</div>';
                    html += '<div class="exp-level">' + data[i].level + '</div>';
                    html += '</div>';
                    html += '</li>';
                }
                $("#scroller ul").html(html);
                myScroll.refresh();
            }
        });
    } else {
        $.ajax({
            type: "get",
            url: apiUrl,
            dataType: 'json',
            async: true,
            success: function(data) {
                //console.log(data.length)
                var html = '';
                for (var i = 0; i < data.length; i++) {
                    html += '<li class="animated bounceInUp">';
                    html += '<div class="img">';
                    html += '<img src="./images/' + data[i].image + '.png" alt="">';
                    html += '</div>';
                    html += '<div class="ppt">';
                    html += '<h3>' + data[i].category + '</h3>';
                    html += '<div class="sk-text">' + data[i].name + '</div>';
                    html += '<div class="sk-level">' + data[i].level + '</div>';
                    html += '</div>';
                    html += '</li>';
                }
                $("#scroller ul").html(html);
                myScroll.refresh();
            }
        });
    }
    myScroll.refresh();
});




//swiper
var swiper = new Swiper('.swiper-container', {
    pagination: '.swiper-pagination',
    paginationClickable: true,
    onInit: function(swiper) { //Swiper2.x的初始化是onFirstInit
        swiperAnimate.swiperAnimateCache(swiper); //隐藏动画元素 
        swiperAnimate.swiperAnimate(swiper); //初始化完成开始动画
    },
    onSlideChangeEnd: function(swiper) {
        swiperAnimate.swiperAnimate(swiper); //每个slide切换结束时也运行当前slide动画
    }
});